/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sigmatrix.h5.dao.DisRebateConditionDao;
import com.sigmatrix.h5.entity.DisRebateCondition;
import com.sigmatrix.h5.enums.ConditionTrigType;
import com.sigmatrix.h5.enums.ConditionType;
import com.sigmatrix.h5.exception.MessageException;
import com.sigmatrix.h5.service.DisRebateConditionService;
import com.sigmatrix.h5.utils.Assert;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.web.mode.RebateActivityInfo;
import com.sigmatrix.sc.jdbc.common.service.ServiceImpl;

/**
 *@ClassName: DisRebateConditionServiceImpl
 *@Description: 触发条件
 *@author MH
 *@date 2017年8月17日 上午9:57:08
 */
@Service
public class DisRebateConditionServiceImpl extends ServiceImpl<DisRebateCondition,Integer> implements DisRebateConditionService{
	
	@Autowired
	private DisRebateConditionDao disRebateConditionDao;
	
	@Override
	public DisRebateCondition findByStrategyId(Integer strategyId) {
		return disRebateConditionDao.findByStrategyId(strategyId);
	}

	@Override
	public boolean isSatisfiedCondition(DisRebateCondition condition, Integer value) {
		validate(condition, value);
		//目前层级扫码的触发条件类型只能是首次扫码对象，以后有了再拓展
		if(ConditionType.FIRST_SCAN_TYPE.value.equals(condition.getType())){
			return judgeFirstScanType(condition, value);
		}else {
			return judgeWriteBackCount(condition, value);
		}
	}
	/**
	 * 
	 *@Title: judgeFirstScanType
	 *@Description: 判断首次扫码对象
	 *@return boolean
	 *@author MH
	 *@date 2017年12月6日 下午3:51:09
	 *@param condition
	 *@param value
	 *@return
	 */
	private boolean judgeFirstScanType(DisRebateCondition condition,Integer value){
		
		if(ConditionTrigType.eq.value.equals(condition.getTrigType())){
			if(condition.getCriticalValue().equals(value)){
				return true;
			}
		}else if(ConditionTrigType.not.value.equals(condition.getTrigType())){
			if(!condition.getCriticalValue().equals(value)){
				return true;
			}
		}else{
			throw new MessageException(String.format("触发条件[%s]类型为首次扫码对象，不支持比对类型[%s]", condition.getId(),condition.getTrigType()));
		}
		return false;
	}
	private void validate(DisRebateCondition condition,Integer value){
		Assert.notNull(value, "值不能为空");
		Assert.notNull(condition, "触发条件为空");
		Assert.notNull(condition.getId(), "触发条件主键不能为空");
		Assert.notNull(condition.getType(), String.format("触发条件类型不能为空%s", condition.getId()));
		Assert.notNull(condition.getTrigType(), String.format("触发条件比对类型不能为空%s", condition.getId()));
		Assert.notNull(condition.getCriticalValue(), String.format("触发条件值不能为空%s", condition.getId()));
	}
	private boolean judgeWriteBackCount(DisRebateCondition condition,Integer value){
		if(ConditionTrigType.eq.value.equals(condition.getTrigType())){
			if(condition.getCriticalValue().equals(value)){
				return true;
			}
		}else if(ConditionTrigType.lt.value.equals(condition.getTrigType())){
			if(condition.getCriticalValue() > value ){
				return true;
			}
		}else if(ConditionTrigType.gt.value.equals(condition.getTrigType())){
			if(condition.getCriticalValue() < value ){
				return true;
			}
		}else if(ConditionTrigType.multiple.value.equals(condition.getTrigType())){
			if(value % condition.getCriticalValue() == 0){
				return true;
			}
		}else if(ConditionTrigType.le.value.equals(condition.getTrigType())){
			if(condition.getCriticalValue() >= value){
				return true;
			}
		}else if(ConditionTrigType.ge.value.equals(condition.getTrigType())){
			if(condition.getCriticalValue() <= value){
				return true;
			}
		}else{
			throw new MessageException(String.format("触发条件[%s]类型为反写次数，不支持比对类型[%s]", condition.getId(),condition.getTrigType()));
		}
		return false;
	}
	
	@Override
	public DisRebateCondition transformCondition(RebateActivityInfo info){
		if(CommonUtils.isEmpty(info.getConditionId())) return null;
		DisRebateCondition condition = new DisRebateCondition();
		condition.setCriticalValue(info.getConditionValue());
		condition.setId(info.getConditionId());
		condition.setTrigType(info.getConditionTrigType());
		condition.setType(info.getConditionType());
		return condition;
	}
}
